package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.base.domain.User;
import cn.wolfcode.car.base.service.IUserService;
import cn.wolfcode.car.business.domain.BusinessStatement;
import cn.wolfcode.car.business.domain.ServiceItem;
import cn.wolfcode.car.business.domain.StatementItem;
import cn.wolfcode.car.business.query.ServiceItemQuery;
import cn.wolfcode.car.business.query.StatementItemQuery;
import cn.wolfcode.car.business.service.IBusinessStatementService;
import cn.wolfcode.car.business.service.IServiceItemService;
import cn.wolfcode.car.business.service.IStatementItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.exception.BusinessException;
import cn.wolfcode.car.common.web.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/business/statementItem")
public class StatementItemController {

    private String prefix="/business/statementItem";

    @Autowired
    private IStatementItemService statementItemService;
    @Autowired
    private IBusinessStatementService businessStatementService;
    @Autowired
    private IServiceItemService serviceItemService;
    @Autowired
    private IUserService userService;

    //由结算单 的消费状态决定去哪个页面
    @RequestMapping("/itemDetail")
    public String itemDetail(Long statementId, Model model){
        //获取当前结算单状态
        BusinessStatement businessStatement = businessStatementService.get(statementId);
        System.out.println(businessStatement);
        //判断该结算单的状态，如果是消费中则跳转到editDetail页面；否则跳转到showDetail页面
        if(BusinessStatement.STATUS_CONSUME.equals(businessStatement.getStatus())){
            //只需要查询当前结算单进行回显，其他的是发送的ajax请求数据的
            model.addAttribute("statement",businessStatement);
            return prefix+"/editDetail";
        }else {
            //显示已结算单的：只需要查询当前结算单进行回显，其他的是发送的ajax请求数据的
            model.addAttribute("statement",businessStatement);
            //还需要查询出关联的收款人信息，这里就另外发送额外sql语句查
            User payee = userService.get(businessStatement.getPayeeId());
            model.addAttribute("payee",payee);
            return prefix+"/showDetail";
        }
    }

    //分页查询当前结算单明细
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StatementItem> query(StatementItemQuery qo){
        TablePageInfo<StatementItem> statementItems = statementItemService.query(qo);
        if(statementItems==null){
            throw new BusinessException("当前没有结算单明细哦！");
        }
        return statementItems;
    }
    //分页查询所有的服务单项
    @RequestMapping("/selectAllSaleOnList")
    @ResponseBody
    public TablePageInfo<ServiceItem> selectAllSaleOnList(ServiceItemQuery qo){
        TablePageInfo<ServiceItem> serviceItemTablePageInfo = serviceItemService.query(qo);
        return serviceItemTablePageInfo;
    }
    //保存结算单+结算单明细:注意处理传过来的json数组的结算单明细
    @RequestMapping("/saveItems")
    @ResponseBody
    public AjaxResult saveItems(@RequestBody List<StatementItem> statementItems){
        statementItemService.saveStatement(statementItems);
        return new AjaxResult().success("保存成功！");
    }
    //确认支付：这里模拟支付成功跳转会showDetail页面
    @RequestMapping("/payStatement")
    @ResponseBody
    public AjaxResult payStatement(Long statementId){
        try {
            statementItemService.updatePayStatus(statementId);
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().error("支付失败！");
        }
        return new AjaxResult().success("支付成功！");
    }


}
